﻿using SuperMap.Data;
using SuperMap.Realspace;
using System;
using System.Collections.Generic;
using WeifenLuo.WinFormsUI.Docking;
using Ycjw.PipeLine.Map.Basic.Conditions;
using Ycjw.PipeLine.Map.Basic.SuperMap;

namespace Ycjw.PipeLine.Map.Tools.Conditions
{
    /// <summary>
    /// 日期查询
    /// </summary>
    public partial class RQSearcher : ConditionPanel
    {
        private LayerWithDataListPanel LayerWithDataListPanel;
        public RQSearcher() : base()
        {
            this.ConditionPanelName = "日期查询";
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            LayerWithDataListPanel = new LayerWithDataListPanel()
            {
                Dock = System.Windows.Forms.DockStyle.Fill
            };
            Scene scene = this.Scene;
            if (scene == null) return;
            LayerWithDataListPanel.Init(scene);
            //LayerWithDataListPanel.LoadToVector();   
            var types = new List<DatasetType>()
            {
                DatasetType.Line,
                DatasetType.Line3D,
                DatasetType.Network,
                DatasetType.Network3D
            };
            LayerWithDataListPanel.LoadTo(types);
            this.uiGroupBoxCXLX.Controls.Add(LayerWithDataListPanel);

            uiGroupBoxCXLX.Width = this.Width / 2;
        }
        public override object Do(DockPanel dockPanel = null)
        {
            var dataset = LayerWithDataListPanel.SelectLayer3DDataset;
            if (dataset == null)
            {
                Sunny.UI.UIMessageTip.ShowError("选择的图层不是矢量图层");
                return null;
            }
            var dataSetVector = dataset.Dataset as SuperMap.Data.DatasetVector;
            bool hasGJField = false;

            for (int i = 0; i < dataSetVector.FieldInfos.Count; i++)
            {
                if (dataSetVector.FieldInfos[i].Name == "敷设年代")
                {
                    hasGJField = true;
                    break;
                }
            }

            if (!hasGJField)
            {
                Sunny.UI.UIMessageTip.ShowError("选择的图层没有敷设年代信息");
                return null;
            }
            DatasetVectorEx dvEx = new DatasetVectorEx(dataSetVector.Name)
            {
                DatasetVector = dataSetVector
            };
            var yearStart = (uiDatePickerRqStart.Value == null ? new DateTime(1990,1,1) : uiDatePickerRqStart.Value).Year;
            var yearEnd = (uiDatePickerRqEnd.Value == null ? new DateTime(1990, 1, 1) : uiDatePickerRqEnd.Value).Year;
            var allYears = "(";
            for(int i = yearStart; i < yearEnd; i++)
            {
                allYears +="'" + i + "',";
            }
            allYears += "'2999')";
            var dt = dvEx.Query(" 敷设年代 in " + allYears);
            dt.TableName = dataset.Caption;
            return dt;
        }


    }
}
